# This CMake file tries to find the the mysql_version.h header
# and to parse it for version and provider strings
# The following variables are set:
# MYSQL_EMBEDDED_VERSION - The MySQL version number
# MYSQL_EMBEDDED_PROVIDER - The MySQL provider e.g. MariaDB
# MYSQL_EMBEDDED_LIBRARIES - The MySQL embedded library
# MYSQL_EMBEDDED_INCLUDE_DIR - Path to MySQL headers

find_file(MYSQL_EMBEDDED_VERSION_H mysql_version.h PATHS ${MYSQL_EMBEDDED_INCLUDE_DIR} PATH_SUFFIXES mysql)
if(MYSQL_EMBEDDED_VERSION_H MATCHES "MYSQL_EMBEDDED_VERSION_H-NOTFOUND")
  message(FATAL_ERROR "Cannot find the mysql_version.h header")
else()
  message(STATUS "Found mysql_version.h: ${MYSQL_EMBEDDED_VERSION_H}")
endif()


file(READ ${MYSQL_EMBEDDED_VERSION_H} MYSQL_EMBEDDED_VERSION_CONTENTS)
string(REGEX REPLACE ".*MYSQL_SERVER_VERSION[^0-9.]+([0-9.]+).*" "\\1" MYSQL_EMBEDDED_VERSION ${MYSQL_EMBEDDED_VERSION_CONTENTS})
string(REGEX REPLACE ".*MYSQL_COMPILATION_COMMENT[[:space:]]+\"(.+)\".*" "\\1" MYSQL_EMBEDDED_PROVIDER ${MYSQL_EMBEDDED_VERSION_CONTENTS})
string(TOLOWER ${MYSQL_EMBEDDED_PROVIDER} MYSQL_EMBEDDED_PROVIDER)
if(MYSQL_EMBEDDED_PROVIDER MATCHES "[mM]aria[dD][bB]")
  set(MYSQL_EMBEDDED_PROVIDER "MariaDB" CACHE INTERNAL "The MySQL provider")
elseif(MYSQL_EMBEDDED_PROVIDER MATCHES "mysql")
  set(MYSQL_EMBEDDED_PROVIDER "MySQL" CACHE INTERNAL "The MySQL provider")
elseif(MYSQL_EMBEDDED_PROVIDER MATCHES "percona")
  set(MYSQL_EMBEDDED_PROVIDER "Percona" CACHE INTERNAL "The MySQL provider")
else()
  set(MYSQL_EMBEDDED_PROVIDER "Unknown" CACHE INTERNAL "The MySQL provider")
endif()
message(STATUS "MySQL version: ${MYSQL_EMBEDDED_VERSION}")
message(STATUS "MySQL provider: ${MYSQL_EMBEDDED_PROVIDER}")

if(NOT MYSQL_EMBEDDED_PROVIDER STREQUAL "MariaDB")
  message(WARNING "Not using a release version of MariaDB server. If this is intentional, please ignore this warning. Otherwise make sure the right libraries are installed and CMake finds the right libraries.")
endif()
if(MYSQL_EMBEDDED_VERSION VERSION_LESS 5.5.41)
  message(WARNING "MySQL version is ${MYSQL_EMBEDDED_VERSION}. Minimum supported version is 5.5.41.")
endif()

if(NOT (MYSQL_EMBEDDED_VERSION VERSION_LESS 10.1))

  # 10.1 needs lzma
  find_library(HAVE_LIBLZMA NAMES lzma)
  if(NOT HAVE_LIBLZMA)
    message(FATAL_ERROR "Could not find liblzma")
  endif()
  set(LZMA_LINK_FLAGS "lzma" CACHE STRING "liblzma link flags")
endif()


if (DEFINED MYSQL_EMBEDDED_LIBRARIES)
  if( NOT (IS_DIRECTORY ${MYSQL_EMBEDDED_LIBRARIES}) )
	debugmsg("MYSQL_EMBEDDED_LIBRARIES is not a directory: ${MYSQL_EMBEDDED_LIBRARIES}")
	if(${CMAKE_VERSION} VERSION_LESS 2.8.12 )
	  set(COMP_VAR PATH)
	else()
	  set(COMP_VAR DIRECTORY)
	endif()
	get_filename_component(MYSQL_EMBEDDED_LIBRARIES ${MYSQL_EMBEDDED_LIBRARIES} ${COMP_VAR})
	debugmsg("MYSQL_EMBEDDED_LIBRARIES directory component: ${MYSQL_EMBEDDED_LIBRARIES}")
  endif()
  debugmsg("Searching for the embedded library at: ${MYSQL_EMBEDDED_LIBRARIES}")
endif()

if(STATIC_EMBEDDED)

  debugmsg("Using the static embedded library...")
  set(OLD_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
  if (DEFINED MYSQL_EMBEDDED_LIBRARIES)
	debugmsg("Searching for libmysqld.a at: ${MYSQL_EMBEDDED_LIBRARIES}")
	find_library(MYSQL_EMBEDDED_LIBRARIES_STATIC libmysqld.a PATHS ${MYSQL_EMBEDDED_LIBRARIES} PATH_SUFFIXES mysql mariadb NO_DEFAULT_PATH)
  else()
	find_library(MYSQL_EMBEDDED_LIBRARIES_STATIC libmysqld.a PATH_SUFFIXES mysql mariadb)
  endif()
  debugmsg("Search returned: ${MYSQL_EMBEDDED_LIBRARIES_STATIC}")

  set(MYSQL_EMBEDDED_LIBRARIES ${MYSQL_EMBEDDED_LIBRARIES_STATIC} CACHE FILEPATH "Path to libmysqld" FORCE)
  set(CMAKE_FIND_LIBRARY_SUFFIXES ${OLD_SUFFIXES})

else()
  debugmsg("Using the dynamic embedded library...")
  set(OLD_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  set(CMAKE_FIND_LIBRARY_SUFFIXES ".so")
  if (DEFINED MYSQL_EMBEDDED_LIBRARIES)
	debugmsg("Searching for libmysqld.so at: ${MYSQL_EMBEDDED_LIBRARIES}")
	find_library(MYSQL_EMBEDDED_LIBRARIES_DYNAMIC mysqld PATHS ${MYSQL_EMBEDDED_LIBRARIES} PATH_SUFFIXES mysql mariadb NO_DEFAULT_PATH)
  else()
	find_library(MYSQL_EMBEDDED_LIBRARIES_DYNAMIC mysqld PATH_SUFFIXES mysql mariadb)
  endif()
  debugmsg("Search returned: ${MYSQL_EMBEDDED_LIBRARIES_DYNAMIC}")
  set(MYSQL_EMBEDDED_LIBRARIES ${MYSQL_EMBEDDED_LIBRARIES_DYNAMIC} CACHE FILEPATH "Path to libmysqld" FORCE)
  set(CMAKE_FIND_LIBRARY_SUFFIXES ${OLD_SUFFIXES})

endif()

unset(MYSQL_EMBEDDED_LIBRARIES_DYNAMIC)
unset(MYSQL_EMBEDDED_LIBRARIES_STATIC)
unset(OLD_SUFFIXES)

check_library_exists(${MYSQL_EMBEDDED_LIBRARIES} pcre_stack_guard ${MYSQL_EMBEDDED_LIBRARIES} HAVE_EMBEDDED_PCRE)

if(HAVE_EMBEDDED_PCRE)
  set(PCRE_LINK_FLAGS "" CACHE INTERNAL "pcre linker flags")
else()
  find_package(PCRE)
  if(PCRE_FOUND)
    set(PCRE_LINK_FLAGS "pcre" CACHE INTERNAL "pcre linker flags")
    message(STATUS "Embedded mysqld does not have pcre_stack_guard, linking with system pcre.")
  else()
    message(FATAL_ERROR "Could not find PCRE libraries.")
  endif()
endif()
if( (${MYSQL_EMBEDDED_LIBRARIES} MATCHES "NOTFOUND") OR (${MYSQL_EMBEDDED_LIBRARIES} MATCHES "NOTFOUND"))
  message(FATAL_ERROR "Library not found: libmysqld. If your install of MySQL is in a non-default location, please provide the location with -DMYSQL_EMBEDDED_LIBRARIES=<path to library>")
else()
  message(STATUS "Using embedded library: ${MYSQL_EMBEDDED_LIBRARIES}")
endif()

# Find the MySQL headers if they were not defined
if(DEFINED MYSQL_EMBEDDED_INCLUDE_DIR)
  list(APPEND CMAKE_INCLUDE_PATH ${MYSQL_EMBEDDED_INCLUDE_DIR})
  find_path(MYSQL_EMBEDDED_INCLUDE_DIR_LOC mysql.h PATHS ${MYSQL_EMBEDDED_INCLUDE_DIR} PATH_SUFFIXES mysql mariadb NO_DEFAULT_PATH)
else()
  find_path(MYSQL_EMBEDDED_INCLUDE_DIR_LOC mysql.h PATH_SUFFIXES mysql mariadb)
endif()

if(${MYSQL_EMBEDDED_INCLUDE_DIR_LOC} MATCHES "NOTFOUND")
  message(FATAL_ERROR "Fatal Error: MySQL headers were not found.")
else()
  set(MYSQL_EMBEDDED_INCLUDE_DIR ${MYSQL_EMBEDDED_INCLUDE_DIR_LOC} CACHE PATH "Path to MySQL headers" FORCE)
  message(STATUS "Using MySQL headers found at: ${MYSQL_EMBEDDED_INCLUDE_DIR}")
endif()

unset(MYSQL_EMBEDDED_INCLUDE_DIR_LOC)

# Find the errmsg.sys file if it was not defied
if( DEFINED ERRMSG )
  debugmsg("Looking for errmsg.sys at: ${ERRMSG}")
  if(NOT(IS_DIRECTORY ${ERRMSG}))
	get_filename_component(ERRMSG ${ERRMSG} PATH)
	debugmsg("Path to file is: ${ERRMSG}")
  endif()
  find_file(ERRMSG_FILE errmsg.sys PATHS ${ERRMSG} NO_DEFAULT_PATH)
  if(${ERRMSG_FILE} MATCHES "NOTFOUND")
	message(FATAL_ERROR "Fatal Error: The errmsg.sys file was not found at ${ERRMSG}")
  else()
	message(STATUS "Using custom errmsg.sys found at: ${ERRMSG_FILE}")
  endif()
else()
  find_file(ERRMSG_FILE errmsg.sys PATHS /usr/share /usr/share/mysql /usr/local/share/mysql PATH_SUFFIXES english mysql/english)
  if(${ERRMSG_FILE} MATCHES "NOTFOUND")
    message(FATAL_ERROR "Fatal Error: The errmsg.sys file was not found, please define the path to it by using -DERRMSG=<path>")
  else()
	message(STATUS "Using errmsg.sys found at: ${ERRMSG_FILE}")
  endif()
endif()
set(ERRMSG ${ERRMSG_FILE} CACHE FILEPATH "Path to the errmsg.sys file." FORCE)
unset(ERRMSG_FILE)
